import {createApp} from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'

// 导入 element-plus
import locale from 'element-plus/es/locale/lang/zh-cn'
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import request from "@/utils/request";

const BASE_URL = 'http://localhost:8080'
//定义全局图片访问路径
const PNG_PATH = '/api/crm/img'
window.BASE_URL = BASE_URL
window.PNG_PATH = PNG_PATH

const app = createApp(App)
// 全局挂载 BASE_URL
app.config.globalProperties.BASE_URL = BASE_URL

// 全局权限检查方法
app.config.globalProperties.$hasPermission = function(permission) {
    // 从localStorage获取权限列表
    const permissions = JSON.parse(localStorage.getItem('perms') || [])

    // 生成权限匹配模式
    const parts = permission.split(':')
    const patterns = parts
        .map((_, i) =>
            parts.slice(0, i + 1).join(':') +
            (i < parts.length - 1 ? ':*' : '')
        )
        .concat('*') // 添加全局通配符

    return patterns.some(p => permissions.includes(p))
}

// 注册 element-plus 图标
app.config.globalProperties.$icons = []
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.config.globalProperties.$icons.push(key)
    app.component(key, component)
}
app.use(store).use(router).use(ElementPlus, {locale}).mount('#app')


//10.定义根据字典code获取对应所有字典项的方法
window.loadDictOptions = (object,dictCode)=>{
   request.get('/api/sys/dict/options/select/'+dictCode).then(res=>{
       object.value = res.data
       //console.log(res.data)
   })
}





